package com.wl.mapper;

import com.wl.pojo.Comment;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CommentMapper {

    /**
     * 对文章发表评论的接口
     */
//    @Insert("insert into comments (content, user_id, post_id) values(#{content}, #{userId}, #{postId})")
    void sendComment(Comment comment);

    /**
     * 根据评论id获取单条评论的详细信息
     */
    @Select("SELECT\n" +
            "    cm.id,\n" +
            "    cm.content,\n" +
            "    cm.create_time,\n" +
            "    u.user_id,\n" +
            "    u.img_url AS commenter_img,\n" +
            "    u.username AS commenter_username,\n" +
            "    (\n" +
            "        SELECT u.username\n" +
            "        FROM comments c\n" +
            "                 JOIN users u ON c.user_id = u.user_id\n" +
            "        WHERE c.id = cm.parent_id\n" +
            "    ) AS reply_to_username,\n" +
            "    cm.like_count,\n" +
            "    cm.parent_id,\n" +
            "    IFNULL(rc.reply_count, 0) AS reply_count\n" +
            "FROM comments cm\n" +
            "         JOIN users u ON cm.user_id = u.user_id\n" +
            "         LEFT JOIN (\n" +
            "    SELECT parent_id, COUNT(*) AS reply_count\n" +
            "    FROM comments\n" +
            "    WHERE parent_id IS NOT NULL\n" +
            "    GROUP BY parent_id\n" +
            ") rc ON cm.id = rc.parent_id\n" +
            "WHERE cm.id = #{id};")
    Comment getCommentInfoByCommentId(Integer id);

    /**
     * 获取文章评论
     */
    @Select("SELECT\n" +
            "    cm.id,\n" +
            "    cm.content,\n" +
            "    cm.create_time,\n" +
            "    u.user_id,\n" +
            "    u.img_url AS commenter_img,\n" +
            "    u.username AS commenter_username,\n" +
            "    (\n" +
            "        SELECT u.username\n" +
            "        FROM comments c\n" +
            "                 JOIN users u ON c.user_id = u.user_id\n" +
            "        WHERE c.id = cm.parent_id\n" +
            "    ) AS reply_to_username,\n" +
            "    cm.like_count,\n" +
            "    cm.parent_id,\n" +
            "    IFNULL(rc.reply_count, 0) AS reply_count\n" +
            "FROM comments cm\n" +
            "         JOIN users u ON cm.user_id = u.user_id\n" +
            "         LEFT JOIN (\n" +
            "    SELECT parent_id, COUNT(*) AS reply_count\n" +
            "    FROM comments\n" +
            "    WHERE parent_id IS NOT NULL\n" +
            "    GROUP BY parent_id\n" +
            ") rc ON cm.id = rc.parent_id\n" +
            "WHERE cm.post_id = #{postId};")
    List<Comment> getPostCommentsByPostId(Integer postId);


    /**
     * 查询当前用户对某篇文章下面评论的点赞情况
     */
    @Select("select c.id from comments c\n" +
            "inner join comment_likes cl on c.id = cl.comment_id\n" +
            "where c.post_id = #{postId} and cl.user_id = #{userId};")
    List<Integer> getUserLikesComment(Integer userId, Integer postId);

    /**
     * 点赞某条评论
     */
    @Insert("insert into comment_likes(user_id, comment_id) values(#{userId}, #{id});")
    void likeComment(Integer userId, Integer id);
    // 将该条评论的点赞数量加一
    @Update("update comments set like_count = like_count + 1 where id = #{id};")
    void likeCountReduce(Integer id);

    /**
     * 取消点赞某条评论
     */
    @Delete("delete from comment_likes where  user_id = #{userId} and comment_id = #{id};")
    void cancelLikeComment(Integer userId, Integer id);
    // 将该条评论的点赞数量减一
    @Update("update comments set like_count = like_count - 1 where id = #{id};")
    void LikeCountIncrease(Integer id);


    /**
     * 删除评论
     * @param ids 要删除评论的id
     */
    void deleteComment(List<Integer> ids);
}
